
clear 

* Load the main regression dataset
use "${output_stata}main_regression.dta", clear 

* Keep the sample used in the main regression (table 3)
reghdfe buy HC_distance_s_brut_t1 diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 share_cdi_t0 if share > 0.01 & share < ., absorb(orig_dest_year) cluster(apgr_1_num code_entry_num)
keep if e(sample) 

* Summarize the share_cdi_t0 variable and export summary statistics
eststo clear
eststo summa: quietly estpost summ share_cdi_t1 if e(sample), d
label var share_cdi_t1 "\% of permanent contracts"

esttab summa using "${export}\stat_summ_share_cdi.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _) /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace


* Create terciles for the friction measure
cap drop q_frictions* gp*
egen q_frictions = xtile(share_cdi_t1), nq(3)
drop if q_frictions == .

* Generate dummy variables for each tercile and interaction terms with HC_distance_s_brut_t1
gen q_frictions1 = (q_frictions == 1)
gen q_frictions2 = (q_frictions == 2)
gen q_frictions3 = (q_frictions == 3)

gen q_frictions1_HC = (q_frictions == 1) * HC_distance_s_brut_t1
gen q_frictions2_HC = (q_frictions == 2) * HC_distance_s_brut_t1
gen q_frictions3_HC = (q_frictions == 3) * HC_distance_s_brut_t1

* Label variables for output tables
label variable q_frictions2 "2nd tercile of Adjustment frictions$\_{\textit{g,n,t-1}}$" 
label variable q_frictions3 "3rd tercile of Adjustment frictions$\_{\textit{g,n,t-1}}$"
label variable q_frictions2_HC "2nd t. Ajd. frict.$\_{\textit{g,n,t-1}}\times$ HC Distance$\_{\textit{g,n,t-1}}$" 
label variable q_frictions3_HC "3rd t. Adj. frict.$\_{\textit{g,n,t-1}}\times$ HC Distance$\_{\textit{g,n,t-1}}$"

* Clear previous estimates and run regressions for each tercile
eststo clear

reghdfe buy HC_distance_s_brut_t1 diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 /*
*/  if q_frictions == 1, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num)
estimates store frictions_1
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"

reghdfe buy HC_distance_s_brut_t1 diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 /*
*/  if q_frictions == 2, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num)
estimates store frictions_2
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"

reghdfe buy HC_distance_s_brut_t1 diversity_t1 size_t1 immo_eff_t1 tresact_eff_t1 vaj_eff_t1 sal_eff_t1 /*
*/  if q_frictions == 3, absorb(orig_dest_year) cluster(apgr_1_num code_entry_num)
estimates store frictions_3
estadd local ctrl "Yes"
estadd local orig_dest_year "Yes"

* Create group variable combining orig_dest_year and q_frictions for further interaction analysis
egen double gp1 = group(orig_dest_year q_frictions)

* Run regression with interactions between terciles and HC_distance_s_brut_t1
reghdfe buy i.q_frictions##c.HC_distance_s_brut_t1 /*
*/ i.q_frictions##c.vaj_eff_t1 i.q_frictions##c.diversity_t1 i.q_frictions##c.size_t1 i.q_frictions##c.immo_eff_t1 i.q_frictions##c.sal_eff_t1 i.q_frictions##c.tresact_eff_t1 /*
*/, absorb(gp1) cluster(apgr_1_num code_entry_num)

* Export results to LaTeX file
esttab frictions_1 frictions_2 frictions_3 using "${export}adjustment_frictions.tex", /*
*/ keep(HC_distance*) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/ alignment(D{.}{.}{-1})  /*     
*/ title(results using `var' as frictions measure) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/ collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig_dest_year r2_a N, fmt(0 0 3 %15.0fc) /*
*/ labels(`"Controls"' `"Sector of Origin $\times$ Entry $\times$ Year FE"'  `"Adjusted \(R^{2}\)"' `"Observations"') /*
*/ layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"   "\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}"))

